package com.tuanbiao.ui.blast;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import com.abiao.lib_utils.StringUtils;
import com.hzjq.data.entity.BlastEntity;
import com.hzjq.lib.cap.bean.ErrorCap;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.BlastController;
import com.tuanbiao.databinding.ActivityBlastBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.ErrorDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.events.BlastErrorEvent;
import com.tuanbiao.events.UploadDataDetailsEvent;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.keyevent.listener.OnBlastKeyDownListener;
import com.tuanbiao.models.BlastModel;
import com.tuanbiao.presenter.BlastPresenter;
import com.tuanbiao.ui.upload.UploadDataDetailsActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class BlastActivity extends BasePresenterActivity<BlastPresenter, ActivityBlastBinding> implements BlastController.View {

    public static void start(Context context) {
        Intent starter = new Intent(context, BlastActivity.class);
        context.startActivity(starter);
    }

    private BaseDialog backDialog;

    @Override
    protected ActivityBlastBinding getBindingView() {
        return ActivityBlastBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.btnExit.setOnClickListener(getBackListener());
        binding.btnCheckBackCopy.setOnClickListener(view -> {
            mPresenter.startUpload();
        });
    }

    @Override
    protected View.OnClickListener getBackListener() {
        return view -> onBackPressed();
    }

    @Override
    public void onBackPressed() {
        if (mPresenter.isBlastSuccess()) {
            super.onBackPressed();
        } else {
            backDialog = new MessageDialog.Builder(getActivity()).setTitle("警告")
                    .setMessage("当前雷管已充入高压，请按相关规定操作！")
                    .setListener(dialog -> {
                        mPresenter.exit();
                    }).show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        KeyEventHelper.getInstance().checkBlastKeyDown(keyCode, new OnBlastKeyDownListener() {
            @Override
            public void onBlast() {
                mPresenter.blast();
            }

            @Override
            public void onCheckErrorCaps() {
                mPresenter.checkErrorCaps();
            }
        });
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected BlastPresenter createPresenter() {
        return new BlastPresenter(new BlastModel(), this);
    }

    @Override
    public void showVoltageElectric(float voltage, float electric) {
        binding.tvVoltage.setText(getString(R.string.blast_voltage, voltage));
        binding.tvCurrent.setText(getString(R.string.blast_current, electric));
    }

    @Override
    public void showProgress(int progress, String msg) {
        binding.wlvCharge.setProgressValue(progress);
        binding.wlvCharge.setCenterTitle(progress + "%");
        binding.tvFinish.setVisibility(View.VISIBLE);
        binding.tvFinish.setTextColor(getCompatColor(R.color.textDanger));
        binding.tvFinish.setText(msg);
    }

    @Override
    public void startBlast() {
        binding.wlvCharge.setWaveColor(getCompatColor(R.color.red));
        binding.wlvCharge.setBorderColor(getCompatColor(R.color.red));
        binding.tvFinish.setTextColor(getCompatColor(R.color.textDanger));
        binding.tvFinish.setVisibility(View.VISIBLE);
        binding.tvFinish.setText(R.string.blast_blast_start);
    }

    @Override
    public void blastFinish(boolean success, String msg) {
        binding.tvFinish.setVisibility(View.VISIBLE);
        binding.tvFinish.setTextColor(success ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.textDanger));
        binding.btnExit.setVisibility(success ? View.GONE : View.VISIBLE);
        binding.wlvCharge.setWaveColor(success ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.red));
        binding.wlvCharge.setBorderColor(success ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.red));
        if (success) {
            binding.btnCheckBackCopy.setVisibility(View.VISIBLE);
            binding.wlvCharge.setCenterTitle("爆破完成");
        } else {
            binding.btnExit.setText("退出");
            binding.wlvCharge.setCenterTitle("爆破未完成");
        }
        binding.tvFinish.setText(msg);
    }

    @Override
    public void startCharge() {
        binding.wlvCharge.setWaveColor(getCompatColor(R.color.red));
        binding.wlvCharge.setBorderColor(getCompatColor(R.color.red));
    }

    @Override
    public void blastFailed(String errorMsg) {
        mPresenter.exitBlast();
        binding.wlvCharge.setCenterTitle("起爆失败！");
        binding.wlvCharge.setWaveColor(getCompatColor(R.color.red));
        binding.wlvCharge.setBorderColor(getCompatColor(R.color.red));
        binding.tvFinish.setText(errorMsg);
        binding.btnExit.setVisibility(View.VISIBLE);
        binding.btnExit.setText("退出");
    }

    @Override
    public void chargeFinish(boolean chargeSuccess, String msg) {
        binding.tvFinish.setVisibility(View.VISIBLE);
        binding.btnExit.setVisibility(chargeSuccess ? View.GONE : View.VISIBLE);
        binding.tvFinish.setTextColor(chargeSuccess ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.textDanger));
        binding.wlvCharge.setWaveColor(chargeSuccess ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.red));
        binding.wlvCharge.setBorderColor(chargeSuccess ? getCompatColor(R.color.textSuccess) : getCompatColor(R.color.red));
        if (chargeSuccess) {
            binding.wlvCharge.setCenterTitle("充电完成");
        } else {
            binding.wlvCharge.setCenterTitle("充电异常");
        }
        if (StringUtils.length(msg) > 40) {
            new MessageDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setAlone(true)
                    .setConfirm("我知道了")
                    .setMessage(msg)
                    .show();
        } else {
            binding.tvFinish.setText(msg);
        }

    }

    @Override
    public void chargeError(String msg) {
        if (backDialog != null && backDialog.isShowing()) {
            backDialog.dismiss();
        }
        ErrorDialog.show(getActivity(), msg, true, new ErrorDialog.OnErrorMenuItemClickListener() {
            @Override
            public void onLookErrorListClick() {
                mPresenter.checkErrorCaps();
            }

            @Override
            public void onExitClick() {
                onBackPressed();
            }

            @Override
            public void onNextClick() {
                mPresenter.forceBlast();
            }
        });
    }

    @Override
    public void chargeFailed(String errorMsg) {
        binding.tvFinish.setTextColor(getCompatColor(R.color.textDanger));
        binding.tvFinish.setText(errorMsg);
        binding.tvFinish.setVisibility(View.VISIBLE);
    }

    @Override
    public void startErrorCap(List<ErrorCap> errorCaps) {
        EventBus.getDefault().postSticky(new BlastErrorEvent(errorCaps));
        BlastCheckErrorActivity.start(getActivity());
    }

    @Override
    public void startUpload(BlastEntity blastEntity) {
        EventBus.getDefault().postSticky(new UploadDataDetailsEvent(blastEntity));
        UploadDataDetailsActivity.start(getActivity());
        killMyself();
    }

    @Override
    protected void showShortCircuit(boolean isClose) {
        if (isClose) {
            mPresenter.cancelCharge();
            mPresenter.exitCharge();
            mPresenter.exitBlast();
            finish();
        }
    }
}